﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Security.Cryptography;

public partial class User_Register : System.Web.UI.Page
{
    MemberBUS memBUS = new MemberBUS();
    DataTable tbCheck = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }
    protected void btnJoinNow_Click(object sender, EventArgs e)
    {
        DateTime Bir;
        if (txtUsername.Text.Equals("") || txtPassword1.Text.Equals("") || txtPassword2.Text.Equals("") || txtID.Text.Equals("") || txtFullname.Text.Equals("") || txtBirhtday.Text.Equals("") || txtPhone.Text.Equals("") || txtAddress.Text.Equals("") || txtEmail.Text.Equals(""))
        {
            lblError.Text = "You must enter enough information";
        }
        else
        {
            lblError.Text = "";
            tbCheck = memBUS.checkUser(txtUsername.Text);
            if (tbCheck.Rows.Count > 0)
            {
                lblErrorUser.Text = "Username is used";
            }
            else
            {
                lblErrorUser.Text = "";
                if (txtPassword1.Text.Equals(txtPassword2.Text))
                {
                    tbCheck = memBUS.checkID(txtID.Text);
                    if (tbCheck.Rows.Count > 0)
                    {
                        lblError.Text = "Identity Card is used";
                    }
                    else
                    {
                        lblError.Text = "";
                        tbCheck = memBUS.checkEmail(txtEmail.Text);
                        if (tbCheck.Rows.Count > 0)
                        {
                            lblError.Text = "Email is used";
                        }
                        else
                        {
                            lblError.Text = "";
                            try
                            {
                                bool check = memBUS.insertMember(txtID.Text, txtFullname.Text, DateTime.Parse(txtBirhtday.Text), ddlGender.SelectedValue, txtPhone.Text, txtAddress.Text, txtEmail.Text, txtUsername.Text,Md5AddSecret(txtPassword1.Text));
                                if (check)
                                {
                                    MultiView1.ActiveViewIndex = 1;
                                }
                                else
                                {
                                    lblError.Text = "Insert Error";
                                }
                            }
                            catch (Exception ex)
                            {
                                lblError.Text = "Format date is wrong";
                            }

                        }
                    }
                }
                else
                {
                    lblErrorUser.Text = "Password same as Retype";
                }
            }
        }
    }
    public string Md5AddSecret(string strChange)
    {
        //Change the syllable into UTF8 code
        byte[] pass = Encoding.UTF8.GetBytes(strChange);
        MD5 md5 = new MD5CryptoServiceProvider();
        string strPassword = Encoding.UTF8.GetString(md5.ComputeHash(pass));
        return strPassword;
    }
}